<template>
  <div>
    <Card>
      <Form ref="formData" :model="formData" :rules="ruleValidate" :label-width="80">
        <FormItem label="Name" prop="name">
          <Input v-model="formData.name" placeholder="Enter your name"></Input>
        </FormItem>
        <FormItem label="Desc" prop="desc">
          <Input v-model="formData.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="Enter something..."></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="handleSubmit('formData')">Submit</Button>
          <Button @click="handleReset('formData')" style="margin-left: 8px">Reset</Button>
        </FormItem>
      </Form>
    </Card>
    <Card>
      <Upload :action="upload" >
        <Button icon="ios-cloud-upload-outline">上传文件</Button>
        &nbsp;&nbsp;&nbsp;&nbsp;点击上传文件
      </Upload>
      <tables ref="tables" editable searchable search-place="top" v-model="fileData" :columns="fileColumns"
              @on-delete="handleDelete"/>
      <Button style="margin: 10px 0;" type="primary" @click="exportExcel">导出为Csv文件</Button>
    </Card>
    <Card>
      <tables ref="tables" editable searchable search-place="top" v-model="tableData" :columns="columns"
              @on-delete="handleDelete"/>
      <Button style="margin: 10px 0;" type="primary" @click="exportExcel">导出为Csv文件</Button>
    </Card>

  </div>
</template>

<script>
  import Tables from '_c/tables'
  import { getDataGrid, saveForm } from '@/api/data'
  export default {
    name: 'form_page',
    components: {
      Tables
    },
    data: function () {
      return {
        columns: [
          { title: 'Name', key: 'name', sortable: true },
          { title: 'Email', key: 'email', editable: true },
          { title: 'Create-Time', key: 'createTime' },
          {
            title: 'Handle',
            key: 'handle',
            options: ['delete'],
            button: [
              (h, params, vm) => {
                return h('Poptip', {
                  props: {
                    confirm: true,
                    title: '你确定要删除吗?'
                  },
                  on: {
                    'on-ok': () => {
                      vm.$emit('on-delete', params)
                      vm.$emit('input', params.tableData.filter((item, index) => index !== params.row.initRowIndex))
                    }
                  }
                }, [
                  h('Button', '自定义删除')
                ])
              }
            ]
          }
        ],
        fileColumns:   [
          { title: '模板', key: 'name', sortable: true }
        ],
        tableData: [],
        data: {},
        formData: {
          name: '',
          mail: '',
          city: '',
          gender: '',
          interest: [],
          date: '',
          time: '',
          desc: ''
        },
        ruleValidate: {
          name: [
            { required: true, message: 'The name cannot be empty', trigger: 'blur' }
          ],
          mail: [
            { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' },
            { type: 'email', message: 'Incorrect email format', trigger: 'blur' }
          ],
          city: [
            { required: true, message: 'Please select the city', trigger: 'change' }
          ],
          gender: [
            { required: true, message: 'Please select gender', trigger: 'change' }
          ],
          interest: [
            { required: true, type: 'array', min: 1, message: 'Choose at least one hobby', trigger: 'change' },
            { type: 'array', max: 2, message: 'Choose two hobbies at best', trigger: 'change' }
          ],
          date: [
            { required: true, type: 'date', message: 'Please select the date', trigger: 'change' }
          ],
          time: [
            { required: true, type: 'string', message: 'Please select time', trigger: 'change' }
          ],
          desc: [
            { required: true, message: 'Please enter a personal introduction', trigger: 'blur' },
            { type: 'string', min: 20, message: 'Introduce no less than 20 words', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleDelete (params) {
        console.log(params)
      },
      exportExcel () {
        this.$refs.tables.exportCsv({
          filename: `table-${(new Date()).valueOf()}.csv`
        })
      },
      mounted () {
        getDataGrid().then(res => {
          this.tableData = res.data
        })
      },
      handleSubmit (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            saveForm(this.formData).then(res => {
              this.$Message.success('Success!')
            })
          } else {
            this.$Message.error('Fail!')
          }
        })
      },
      handleReset (name) {
        this.$refs[name].resetFields()
      }
    }
  }
</script>

<style>

</style>
